<?php
session_start();

require_once 'database/chat_handler.php';
require_once 'database/notif_handler.php';


if (isset($_POST['SubmitUserOnline'])){
    $sel2 = $_POST['selectUserOnline'];
    //handle disini supaya kalo ada yang sama jadi cuma diambil yg pertama
    $sel = array_unique($sel2);

    $_SESSION['people'] = $sel;
    //masukin ke db
    $ch = new chat_handler();
    $sel[count($sel)] = $_SESSION['Username'];

    $_SESSION['gid'] = $ch->create_group($sel);

    //disini kirim notif ke semua $sel
    $no = new notif_handler();

    for ($i=0; $i<count($sel)-1; $i++){
        $usr_tujuan = $sel[$i];
        $no->add_notification($_SESSION['Username'], $usr_tujuan, "chat", 0, $_SESSION['gid']);
    }
	$tampil=true;
}

if (isset($_GET['setgid'])){
    $temp = $_GET['setgid'];
    
    $_SESSION['gid'] = $temp;

    //set juga peoplenya

    $ch = new chat_handler();

    $ch->getMemberGroup($_SESSION['gid'], $_SESSION['Username']);

    $_SESSION['people'] = $ch->nama;
}

//echo $_GET['setgid'];

if ($_GET['set'] == 1){
    $no = new notif_handler();
    $notif_id = $_GET['notifid'];
    $no->set_read($notif_id);


}


if (isset($_SESSION['gid'])){
    ?>
    <script type="text/javascript" src="script/chat.js"></script>
    <script type="text/javascript">
        viewChatTextArea();
        //

    </script>
    <?php
}

if (isset($_POST['submitChat'])){
    $ch = new chat_handler();

    $ch->send_message($_SESSION['gid'], $_SESSION['Username'], $_POST['chatText']);
	$tampil = true;
}

?>

<html>
    <head>
        <link rel="stylesheet" href="style/style.css" />
        <link rel="stylesheet" href="style/chat.css" />

	<script type="text/javascript" src="script/validator.js"></script>
	<script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <script type="text/javascript" src="script/chat.js"></script>
        <title>Chat</title>

    </head>
    <body onLoad="onloadPageChat()" >

        <div id="container">
        <div id="headerBody">
            <form id="Search" name="Search" method ="GET" action="search_result.php">

                <input type="text" id="SearchBar" name="SearchBar"/>

                <input type="submit" id="SearchOk" name="SearchOk" value="Search"/>
            </form>
            <?php if ($_SESSION['Username'] != null) {
            ?>
                <a id="headerLink" href="javascript:viewHome();" id="headerLink">Home</a>
                <a id="headerLink" href="javascript:showPopupProfile();" id="headerLink">Profile</a>
                <a id="headerLink" href="javascript:viewMessage();" id="headerLink">Message</a>
                <a id="headerLink" href="javascript:viewUploader();" id="headerLink">Uploader</a>
                <a id="headerLink" href="javascript:viewGallery();" id="headerLink">Gallery</a>
                <a href="database/login.php?logout=1" id="logout_link" >Logout</a>
                <a href="chat.php" id="logout_link" >Chat</a>
                <a href="javascript:viewProfile();" id="logout_link" ><?php echo $_SESSION['Username'];?></a>


                <a href="javascript:showNotif('<?php echo $_SESSION['Username']; ?>');" id="headerLink">Notification</a>

                <!-- tampilin jumlah notif -->
                <script type="text/javascript">
                </script>
                <span id="notif_number">

                </span>
                <div id="profile_popup">
                </div>
                <div id="notif">
                    <div id="notif2">
                    </div>
                </div>
                <a href="profile.php" id="logout_link" ><?php echo $u; ?></a>
            <?php } else { ?>
                <a href="index.php" id="logout_link" >Login</a>
            <?php
            }
            ?>
        </div>
        <div id=""><img src="images/flickertiew_logo.gif" alt=""/></div>
        <!-- CONTENT dimulai disini -->
        <div id="centerBody">
        <div id ="content">
        <div id="user_online">
            <?php
            $ch = new chat_handler();

            $ch->get_user_online($_SESSION['Username']);
            ?>
        </div>
        <div id="chat_field">
            <!-- foto2 yg online -->
            <div id="_title2">User online : </div>
            <br/>
            <div id="userOnline">
                <form method="post" action="chat.php">
<?php
        //yang follower
        echo "Followers : ";
            if (count($ch->img_url[0]) != 0){
                for ($i = 0; $i < count($ch->img_url[0]); $i++) {
    ?>
                        <!-- checkbox -->
                        <input type="checkbox" name="selectUserOnline[]" value="<?php echo $ch->username[0][$i]; ?>" />
                        <img src="<?php echo $ch->img_url[0][$i]; ?>" alt="user" width="50" height="50"/>
    <?php
                    }
            }
            else{
                echo "No user online.";
            }
            echo "<br/>";

                //yang following
          echo "Followings : ";
          if (count($ch->img_url[1]) != 0){
                for ($i = 0; $i < count($ch->img_url[1]); $i++) {
?>
                    <!-- checkbox -->
                    <input type="checkbox" name="selectUserOnline[]" value="<?php echo $ch->username[1][$i]; ?>" />
                    <img src="<?php echo $ch->img_url[1][$i]; ?>" alt="user" width="50" height="50"/>
<?php
                }
          }
          else{
              echo "No user online.";
          }

?>
                    <br/><!-- start chat -->
                    <input type="submit" name="SubmitUserOnline" value="Start Chat!"/>


                </form>
            </div>
            <div id="title_chat">
                You are currently chatting with
                <?php
                    for ($i=0; $i<count($_SESSION['people']); $i++){
                        if ($ch->nama[0][$i] != null){
                            echo $ch->nama[0][$i]. ", ";
                        }
                        if ($ch->nama[1][$i] != null){
                            echo $ch->nama[1][$i]. ", ";
                        }
                        
                    }
                    echo "<br/>";
                ?>
            </div>
            <div id="formChat">
                <?php
					if ($tampil) {
						?>
						<div id="form_chat" >
						<form method="post" action="chat.php">
							<textarea cols="60" rows ="4" name="chatText" id="chatText"></textarea>
							<input type="submit" name="submitChat" value="Submit"/>
						</form>
						</div>
						<?php
					}
				?>
            </div>
            <div id="bodyChat">
            
            </div>
            <div id="endChat">
                <input type="button" value="End Chat" name="endChat" onclick="javascript:endChat();"/>
            </div>
            
            <!--

            -->
        </div>
        </div>
        </div>
        <div id="footerBody" class="group_content">
                <div id="footer" align="center">
                    Copyright &copy; 2011<br/>
                    Created by <a href="http://www.facebook.com/Gunawan.Robert" id="headerLink">Robert Gunawan</a>
                                                    , <a href="http://www.facebook.com/danz3m" id="headerLink">Danang Tri Massandy</a>,
                    <a href="http://www.facebook.com/achmad.giovani" id="headerLink">Achmad Giovani</a>
                </div>
            </div>
        </div>


    </body>

</html>
